#!/bin/bash
set -e;
openfrp_version='0.51.3_954e51ff_20230904'
project_name='openfrp'
echo "copy files......"
whoname=`whoami`
sudo mkdir -p /opt/${project_name}/logs;sudo chmod -R 755 /opt/${project_name};
sudo chown -R ${whoname} /opt/${project_name};
if [ -f /opt/${project_name}/frpc_linux_arm64 ];then
	sudo systemctl stop ${project_name}||echo 'skip'
	rm -f /opt/${project_name}/frpc_linux_arm64;
fi;
wget https://dl.zyghit.cn/client/OpenFRP_${openfrp_version}/frpc_linux_arm64.tar.gz -O /opt/${project_name}/frpc_linux_arm64.tar.gz
tar -xf /opt/${project_name}/frpc_linux_arm64.tar.gz -C /opt/${project_name}/;
cp st*.sh /opt/${project_name}/;
cp *.service /opt/${project_name}/;
chmod -R 755 /opt/${project_name}/;

sed -i "s/User=.*/User=${whoname}/g" /opt/${project_name}/${project_name}.service;
sudo cp /opt/${project_name}/${project_name}.service /lib/systemd/system/${project_name}.service;
sudo systemctl daemon-reload;



if [ -e /opt/${project_name}/openfrp-tunnel ]; then
    echo "There is having /opt/${project_name}/openfrp-tunnel , ignore init openfrp-tunnel!"
	echo "start services......"
	sudo systemctl enable --now ${project_name};
else
    cp openfrp-tunnel /opt/${project_name}/;
	echo "please config /opt/${project_name}/openfrp-tunnel, then to start [/opt/${project_name}/start.sh]"

fi;


